//
//  SharePlugin.m
//  HelloWorld
//
//  Created by LYK on 15/6/30.
//
/////
#define UNINSTALLWEIXIN @"未安装微信"
#define UNINSTALLSINA @"未安装新浪"
#define UNINSTALLQQ @"未安装QQ"

#import "SharePlugin.h"
#import "ShareListView.h"
#import "ShareUI.h"

@interface SharePlugin () <ShareDelegate,ShareUIDelegate>
@property (nonatomic,strong) CDVInvokedUrlCommand *shareCommand;
@end

@implementation SharePlugin

- (void)shareToSina:(CDVInvokedUrlCommand *)command
{
    
    [self.commandDelegate runInBackground:^{
        OSMessage *message = [[OSMessage alloc] init];
        NSDictionary *commandDic = command.arguments[0];
        message.title = commandDic[@"title"];
        message.image  = [NSData dataWithContentsOfURL:[NSURL URLWithString:commandDic[@"image"]]];

        message.link = commandDic[@"link"];
        
        __block CDVPluginResult *pluginResult = nil;
        if ([OpenShare isWeiboInstalled]) {
            [OpenShare shareToWeibo:message Success:^(OSMessage *message) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"success"];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            } Fail:^(OSMessage *message, NSError *error) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                
            }];
        }else{
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:UNINSTALLSINA];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
    }];
}

- (void)shareToWeixinFavorite:(CDVInvokedUrlCommand *)command
{
    [self.commandDelegate runInBackground:^{
        OSMessage *message = [[OSMessage alloc] init];
        
        NSDictionary *commandDic = command.arguments[0];
        message.title = commandDic[@"title"];
        message.image  = [NSData dataWithContentsOfURL:[NSURL URLWithString:commandDic[@"image"]]];
        
        message.link = commandDic[@"link"];
        
        
        __block CDVPluginResult *pluginResult = nil;
        if ([OpenShare isWeixinInstalled]) {
            [OpenShare shareToWeixinFavorite:message Success:^(OSMessage *message) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"success"];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            } Fail:^(OSMessage *message, NSError *error) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            }];
        }else{
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:UNINSTALLWEIXIN];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
        
    }];
}

- (void)shareToWeixinSession:(CDVInvokedUrlCommand *)command
{
    [self.commandDelegate runInBackground:^{
        OSMessage *message = [[OSMessage alloc] init];
        NSDictionary *commandDic = command.arguments[0];
        message.title = commandDic[@"title"];
        message.image  = [NSData dataWithContentsOfURL:[NSURL URLWithString:commandDic[@"image"]]];
        
        message.link = commandDic[@"link"];
        
        __block CDVPluginResult *pluginResult = nil;
        if ([OpenShare isWeixinInstalled]) {
            [OpenShare shareToWeixinSession:message Success:^(OSMessage *message) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"success"];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            } Fail:^(OSMessage *message, NSError *error) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            }];
        }else{
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:UNINSTALLWEIXIN];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
       
    }];
}
- (void)shareToWeixinTimeline:(CDVInvokedUrlCommand *)command
{
    [self.commandDelegate runInBackground:^{
        OSMessage *message = [[OSMessage alloc] init];
        NSDictionary *commandDic = command.arguments[0];
        message.title = commandDic[@"title"];
        message.image  = [NSData dataWithContentsOfURL:[NSURL URLWithString:commandDic[@"image"]]];
        
        message.link = commandDic[@"link"];
        
        
        __block CDVPluginResult *pluginResult = nil;
        if ([OpenShare isWeixinInstalled]) {
            [OpenShare shareToWeixinTimeline:message Success:^(OSMessage *message) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"success"];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            } Fail:^(OSMessage *message, NSError *error) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            }];
        }else{
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:UNINSTALLWEIXIN];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
        
        
    }];
}

- (void)shareToQQFriends:(CDVInvokedUrlCommand *)command{
    [self.commandDelegate runInBackground:^{
        OSMessage *message = [[OSMessage alloc] init];
        NSDictionary *commandDic = command.arguments[0];
        message.title = commandDic[@"title"];

        message.image  = [NSData dataWithContentsOfURL:[NSURL URLWithString:commandDic[@"image"]]];
        
        message.link = commandDic[@"link"];
        
        if (!commandDic[@"desc"] || commandDic[@"desc"] == [NSNull null]) {
            message.desc = @"shareToQQFriends";
        }
        
        else{
            message.desc = commandDic[@"desc"];
        }
        
        __block CDVPluginResult *pluginResult = nil;
        if ([OpenShare isQQInstalled]) {
            [OpenShare shareToQQFriends:message Success:^(OSMessage *message) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"success"];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                
            } Fail:^(OSMessage *message, NSError *error) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                
            }];
            
        }else{
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:UNINSTALLQQ];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
        
        
    }];
}

- (void)shareToQQZone:(CDVInvokedUrlCommand *)command{
    [self.commandDelegate runInBackground:^{
        OSMessage *message = [[OSMessage alloc] init];
        NSDictionary *commandDic = command.arguments[0];
        message.title = commandDic[@"title"];
        message.image  = [NSData dataWithContentsOfURL:[NSURL URLWithString:commandDic[@"image"]]];
        
        message.link = commandDic[@"link"];
        
        if (!commandDic[@"desc"] || commandDic[@"desc"] == [NSNull null]) {
            message.desc = @"shareToQQZone";
        }
        
        else{
            message.desc = commandDic[@"desc"];
        }

        
        __block CDVPluginResult *pluginResult = nil;
        
        if ([OpenShare isQQInstalled]) {
            
            [OpenShare shareToQQZone:message Success:^(OSMessage *message) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"success"];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                
            } Fail:^(OSMessage *message, NSError *error) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                
            }];
        }
        
        else{
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:UNINSTALLQQ];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
    }];
}

- (void)shareToQQFavorites:(CDVInvokedUrlCommand *)command{
    [self.commandDelegate runInBackground:^{
        OSMessage *message = [[OSMessage alloc] init];
        NSDictionary *commandDic = command.arguments[0];
        message.title = commandDic[@"title"];
        message.image  = [NSData dataWithContentsOfURL:[NSURL URLWithString:commandDic[@"image"]]];
        
        message.link = commandDic[@"link"];
        
        if (!commandDic[@"desc"] || commandDic[@"desc"] == [NSNull null]) {
            message.desc = @"shareToQQFavorites";
        }
        
        else{
            message.desc = commandDic[@"desc"];
        }
        
        __block CDVPluginResult *pluginResult = nil;
        if ([OpenShare isQQInstalled]) {
            [OpenShare shareToQQFavorites:message Success:^(OSMessage *message) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"success"];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            } Fail:^(OSMessage *message, NSError *error) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            }];

        }
        
        else{
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:UNINSTALLQQ];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
    }];
}

/**
 *  打开分享选择界面
 *
 *  @param command 前端返回的参数
 */


- (void)share:(CDVInvokedUrlCommand *)command{
//    ShareListView *list = [[ShareListView alloc] init];
//    list.delegate = self;
//    [list show:command];
    
    ShareUI *shareUI=[ShareUI sharedInstance];
    shareUI.delegate=self;
    NSArray *iamges=@[@"share_sinaWeibo@2x",@"share_wechat_session@2x",@"share_wechat_timeline@2x",@"share_wechat_favorite@2x",@"share_qq@2x",@"share_qq_zone@2x",@"share_qq_favorite@2x"];
  
    self.shareCommand=command;
    
    NSArray *titlelist=@[@"新浪微博",@"微信好友",@"微信说说",@"微信收藏",@"QQ",@"QQ空间",@"QQ收藏",@"",];
    NSArray *tagList=@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",];
    
    [shareUI showShareWindowWithImageList:iamges titleList:titlelist tagList:tagList];
}
#pragma mark ShareUIdelegate
-(void)shareBtClickedWithTag:(NSInteger)tag{
    
    switch (tag) {
        case 0://SHARE_SINAWEIBO
            [self shareToSina:self.shareCommand];
            break;
        case 1://SHARE_WEIXIN_SESSION
            [self shareToWeixinSession:self.shareCommand];
            break;
        case 2://SHARE_WEIXIN_TIMELINE
            [self shareToWeixinTimeline:self.shareCommand];
            break;
        case 3:
            [self shareToWeixinFavorite:self.shareCommand];
            break;
        case 4:
            [self shareToQQFriends:self.shareCommand];
            break;
        case 5:
            [self shareToQQZone:self.shareCommand];
            break;
        case 6:
            [self shareToQQFavorites:self.shareCommand];
            break;
            
        default:
            break;
    }


}

@end
